<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_type_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/item_types/tcms_menu_item_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class Menu_Item_VPage_Manage_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_menu/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('menu');
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = $this->create_view('not_exist');
		$this->add_view($v2);

		$manage_event = new Web_Event_Simple_Int('manage', Tangra_Parameter_Method::GET, 'manage');
		$this->wed->add_wed_event_action_pair($manage_event, 'manage_event', 20);

		$parent_event = new Web_Event_Simple_Int('parent', Tangra_Parameter_Method::GET, 'parent');
		$this->wed->add_wed_event_action_pair($parent_event, 'parent_event', 30);

		$select_event_comp = new Web_Event_Composite('select');
		$select_event = new Web_Event_Simple_Int('select', Tangra_Parameter_Method::GET, 'select');
		$select_event_comp->add_wes($select_event);
		$select_event_comp->add_wes($manage_event);

		$this->wed->add_wed_event_action_pair($select_event_comp, 'select_event');
	}


	protected function manage_event($menu_item_id) {
		$dbc = $this->get_dbc();

		$item = new TCMS_Menu_Item_VPage_DBC();
		if ($item->load_by_id($dbc, $menu_item_id)) {
			$this->export('item', $item);

			$type = new TCMS_Menu_Item_Type_DBC();
			if(!$type->load_by_hid($dbc, 'vpage')) {
				throw new Tangra_Module_Exception('Cannot load TCMS_Menu_Item_Type_DBC with hid = "vpage"');
			}
			$this->export('type', $type);


			$menu = new TCMS_Menu_DBC();
			if (!$menu->load_by_id($dbc, $item->get_menu())) {
				throw new Tangra_Module_Exception('Cannot load TCMS_Menu_DBC with id = '.$item->get_menu());
			}
			$this->export('tcms_menu', $menu);
			$this->set_transit_var_value('menu', $item->get_menu());

			$vpage_id = $item->get_vpage_id();
			if ($vpage_id) {
				$vpage = new TCMS_VPage_DBC();
				if ($vpage->load_by_id($dbc, $vpage_id)) {
					$this->export('vpage', $vpage);
				}
			}

			$ret = urlencode('admin/modules/tangra_cms/menu/item_types/menu_item_vpage_manage.php?manage='.$menu_item_id);
			$this->export('ret', $ret);


			$view = $this->get_view('default');
		} else {
			$view = $this->get_view('not_exist');
		}

		return $view;
	}


	protected function parent_event($parent) {
		$dbc = $this->get_dbc();
		$type = new TCMS_Menu_Item_Type_DBC();
		if(!$type->load_by_hid($dbc, 'vpage')) {
			throw new Tangra_Module_Exception('Cannot load TCMS_Menu_Item_Type_DBC with hid = "vpage"');
		}

		$view = $this->create_redirect_view('admin/modules/tangra_cms/menu/menu_item_edit.php', array('menu_item_add_new' => 1, 'type' => $type->get_id(), 'parent' => $parent , 'ret' => 'admin/modules/tangra_cms/menu/item_types/menu_item_vpage_manage.php'));

		return $view;
	}

	protected function default_event() {
		throw new Tangra_Module_Exception('Unexpected parameters');
	}


	protected function select_event($params) {
		$menu_item_id = $params['manage'];
		$vpage_id = $params['select'];

		$dbc = $this->get_dbc();
		if (!TCMS_Menu_Item_VPage_DBC::exists($dbc, $menu_item_id, $vpage_id)) {
			$menu_item_vpage = new TCMS_Menu_Item_VPage_DBC();
			$menu_item_vpage->load_by_id($dbc, $menu_item_id);
			$menu_item_vpage->set_vpage_id($vpage_id);

			try {
				$dbc->start_trans();
				$menu_item_vpage->save($dbc);
				TCMS_Menu_DBC::menu_changed($dbc, $menu_item_vpage->get_menu(), $this->get_user()->get_id());
				$dbc->complete_trans();
			} catch (Exception $e) {
				$dbc->fail_trans();
				$dbc->complete_trans();
				throw $e;
			}
		}

		return $this->manage_event($menu_item_id);
	}
}